//
//  ByUIChangePwdVC.m
//  itboye_shop_ios
//
//  Created by justiOS on 17/2/14.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIChangePwdVC.h"
#import "ByApiUser.h"
@interface ByUIChangePwdVC ()
@property (nonatomic, strong) UITextField *mobleTF;         // 旧密码
@property (nonatomic, strong) UITextField *pswTF;           // 新密码
@property (nonatomic, strong) UITextField *pswTF2;          // 新密码再一次
@property (nonatomic, strong) UIButton *loginButton;
@end

@implementation ByUIChangePwdVC

#pragma mark getter

- (UITextField *)mobleTF {
    if (!_mobleTF) {
        _mobleTF = [[UITextField alloc] init];
        _mobleTF.placeholder = @"请输入旧密码";
        _mobleTF.font = FONT(14);
        _mobleTF.tintColor = BY_THEME_COLOR;
        _mobleTF.leftViewMode = UITextFieldViewModeAlways;
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
        leftLabel.font = FONT(14);
        leftLabel.text = @"旧密码";
        leftLabel.textAlignment = NSTextAlignmentCenter;
        _mobleTF.leftView = leftLabel;
        _mobleTF.secureTextEntry = YES;
        [_mobleTF addTarget:self action:@selector(mobleTFChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _mobleTF;
}

- (UITextField *)pswTF {
    if (!_pswTF) {
        _pswTF = [[UITextField alloc] init];
        _pswTF.placeholder = @"请输入新密码";
        _pswTF.tintColor = BY_THEME_COLOR;
        _pswTF.font = FONT(14);
        _pswTF.leftViewMode = UITextFieldViewModeAlways;
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
        leftLabel.font = FONT(14);
        leftLabel.textAlignment = NSTextAlignmentCenter;
        leftLabel.text = @"新密码";
        _pswTF.leftView = leftLabel;
        _pswTF.secureTextEntry = YES;
        [_pswTF addTarget:self action:@selector(mobleTFChange:) forControlEvents:UIControlEventEditingChanged];
        
    }
    return _pswTF;
}

- (UITextField *)pswTF2 {
    if (!_pswTF2) {
        _pswTF2 = [[UITextField alloc] init];
        _pswTF2.placeholder = @"请再次输入新密码";
        _pswTF2.tintColor = BY_THEME_COLOR;
        _pswTF2.font = FONT(14);
        _pswTF2.leftViewMode = UITextFieldViewModeAlways;
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
        leftLabel.font = FONT(14);
        leftLabel.textAlignment = NSTextAlignmentCenter;
        leftLabel.text = @"新密码";
        _pswTF2.leftView = leftLabel;
        _pswTF2.secureTextEntry = YES;
        [_pswTF2 addTarget:self action:@selector(mobleTFChange:) forControlEvents:UIControlEventEditingChanged];
        
    }
    return _pswTF2;
}



//- (UIButton *)seeButton {
//    if (!_seeButton) {
//        _seeButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_seeButton setImage:[UIImage imageNamed:@"Register_eyec"] forState:UIControlStateNormal];
//        [_seeButton setImage:[UIImage imageNamed:@"Register_eyeo"] forState:UIControlStateSelected];
//        [_seeButton addTarget:self action:@selector(changeSecureStatue:) forControlEvents:UIControlEventTouchUpInside];
//    }
//    return _seeButton;
//}


- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginButton.layer.cornerRadius = 3;
        _loginButton.backgroundColor = BY_THEME_COLOR;
        [_loginButton setTitle:@"确认修改" forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _loginButton;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    [self addLeftBackBarButtonItem];
    [self createView];
    self.view.userInteractionEnabled = true;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEmptyAction:)];
    [self.view addGestureRecognizer:tap];
}

- (void)createView {
    
    self.mobleTF.frame = CGRectMake(0, 0, SCREEN_WIDTH - 10, 40);
    self.pswTF.frame = CGRectMake(0, 40, SCREEN_WIDTH - 10, 40);
    self.pswTF2.frame = CGRectMake(0, 80, SCREEN_WIDTH - 10, 40);
    [self.view addSubview:self.mobleTF];
    [self.view addSubview:self.pswTF];
    [self.view addSubview:self.pswTF2];
    
    
    //    self.seeButton.frame = CGRectMake(SCREEN_WIDTH - 30, 50, 20, 20);
    //    [self.view addSubview:self.seeButton];
    
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(10, 40, SCREEN_WIDTH - 10, 1)];
    line1.backgroundColor = rgba(235, 235, 237, 1);
    [self.view addSubview:line1];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, 1)];
    line2.backgroundColor = rgba(235, 235, 237, 1);
    [self.view addSubview:line2];
    
    UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(0, 120, SCREEN_WIDTH, 1)];
    line3.backgroundColor = rgba(235, 235, 237, 1);
    [self.view addSubview:line3];
    
    self.loginButton.frame = CGRectMake(10, line3.bottom + 20, SCREEN_WIDTH - 20, 40);
    [self.view addSubview:self.loginButton];
    
}

#pragma mark 点击事件
- (void)tapEmptyAction:(UITapGestureRecognizer *)tap {
    
    [UIView animateWithDuration:0.4 animations:^{
        [self.pswTF resignFirstResponder];
        [self.mobleTF resignFirstResponder];
        [self.pswTF2 resignFirstResponder];
    }];
}

//- (void)deleAction1:(UIButton *)button {
//    self.mobleTF.text = nil;
//    button.hidden = YES;
//}
//
//- (void)deleAction2:(UIButton *)button {
//    self.pswTF.text = nil;
//    button.hidden = YES;
//}

//- (void)changeSecureStatue:(UIButton *)button {
//    button.selected = !button.selected;
//    _pswTF.secureTextEntry = !_pswTF.secureTextEntry;
//}

- (void)loginAction:(UIButton *)button {
    
    if ([self.mobleTF.text isStringNull]) {
        [ByCoreToolAlert showOnlyText:@"请输入旧密码"];
        return;
    }
    if ([self.pswTF.text isStringNull]) {
        [ByCoreToolAlert showOnlyText:@"新密码不能为空"];
        return;
    }
    if ([self.pswTF2.text isStringNull]) {
        [ByCoreToolAlert showOnlyText:@"新密码不能为空"];
        return;
    }
    
    if (self.mobleTF.text.length < 6 || self.pswTF2.text.length < 6 || self.pswTF.text.length < 6) {
        [ByCoreToolAlert showOnlyText:@"密码长度为6-16字符"];
        return;
    }
    
    
    if (![self.pswTF.text isEqualToString:self.pswTF2.text]) {
        [ByCoreToolAlert showOnlyText:@"新密码两次输入不一致"];
        return;
    }
    
    
    ByNetParamsModel *pm = [[ByNetParamsModel alloc] init];
    pm.s_id = BY_USER.s_id;
    pm.uid = BY_USER.uid;
    pm.old_psw = self.mobleTF.text;
    pm.password = self.pswTF.text;
    [ByApiUser changePwd:pm success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showSuccessWithText:result.msg];
        
        ByModelUser *user = BY_USER;
        user.password = self.pswTF.text;
        [ByCacheUser setUserInfo:user];
        
        self.mobleTF.text = nil;
        self.pswTF.text = nil;
        self.pswTF2.text = nil;
        [UIView animateWithDuration:0.4 animations:^{
            [self.pswTF resignFirstResponder];
            [self.mobleTF resignFirstResponder];
            [self.pswTF2 resignFirstResponder];
        }];
        //        NSLog(@"%@",BY_USER.password);
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}





- (void)mobleTFChange:(UITextField *)textField {
    if (textField.text.length > 16) {
        textField.text = [textField.text substringToIndex:16];
        [ByCoreToolAlert showOnlyText:@"密码最多16位"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
